﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAccessLogic;
using System.Web.Security;
using System.Data;

namespace BusinessLogic
{
    [Serializable]
    public class Phone : Table
    {
        #region Fields
        private PhoneType _phoneType;
        private string _number;
        private string _ext;
        private bool _isPrimary;
        private bool _isActive;
        #endregion

        #region Methods
        public Phone() { }

        public Phone(string number)
        {
            _number = number;
        }

        public bool CreateForConversion(Guid constituentID)
        {
            try
            {
                this._id = PhoneDataAccess.CreateForConversion(this, constituentID);

                if (this._id != Guid.Empty)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }

            return false;
        }

        public bool Create(Guid constituentID)
        {
            try
            {
                this._id = PhoneDataAccess.Create(this, constituentID);

                if (this._id != Guid.Empty)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }

            return false;
        }

        public void Delete()
        {
            PhoneDataAccess.Delete(this._id);
        }
        
        public static List<Phone> GetActiveByConstituentID(Guid constituentID)
        {
            return PhoneDataAccess.GetActiveByConstituentID(constituentID);
        }

        public static List<Phone> GetAllByConstituentID(Guid constituentID)
        {
            return PhoneDataAccess.GetAllByConstituentID(constituentID);
        }

        public static Phone GetPrimaryByConstituentID(Guid constituentID)
        {
            List<Phone> phones = PhoneDataAccess.GetPrimaryByConstituentID(constituentID);

            if((phones != null) && (phones.Count > 0))
            {
                return phones[0];
            }
            else
            {
                return null;
            }
        }

        public void Update(Guid constituentID) 
        {
            PhoneDataAccess.Update(this, constituentID);
        }
        #endregion

        #region Properties
        public bool IsActive
        {
            get
            {
                return _isActive;
            }
            set
            {
                _isActive = value;
            }
        }

        public bool IsPrimary
        {
            get
            {
                return _isPrimary;
            }
            set
            {
                _isPrimary = value;
            }
        }
                
        public PhoneType PhoneType
        {
            get
            {
                return _phoneType;
            }
            set
            {
                _phoneType = value;
            }
        }

        public string PhoneTypeAsString
        {
            get 
            {
                if(_phoneType != null)
                {
                    return _phoneType.Description;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public string Number
        {
            get
            {
                return _number;
            }
            set
            {
                _number = value;
            }
        }

        public string Extension
        {
            get
            {
                return _ext;
            }
            set
            {
                _ext = value;
            }
        }

        public string NumberWithExtension
        {
            get 
            {
                if(string.IsNullOrWhiteSpace(_number))
                {
                    return string.Empty;
                }
                else
                {
                    if(!string.IsNullOrWhiteSpace(_ext))
                    {
                        return string.Concat(_number + " ext. " + _ext);
                    }
                    else
                    {
                        return _number;
                    }
                }
            }
        }

        public string FormattedNumberWithExtension
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_number) && string.IsNullOrWhiteSpace(_ext))
                {
                    return "";
                }
                //else if(!string.IsNullOrWhiteSpace(_ext))
                //{
                //    return string.Concat("ext. ", _ext);
                //}
                else
                {
                    string number = new string(_number.Where(c => char.IsDigit(c)).ToArray());
                    string areacode, prefix, suffix = string.Empty;

                    switch (number.Count())
                    {                        
                        case 7:
                            prefix = number.Substring(0, 3);
                            suffix = number.Substring(3);
                            number = string.Concat(prefix, "-", suffix);
                            break;
                        case 10:
                            areacode = number.Substring(0, 3);
                            prefix = number.Substring(3, 3);
                            suffix = number.Substring(6);
                            number = string.Concat("(", areacode, ")", prefix, "-", suffix);
                            break;
                        case 11:
                            if(number.Substring(0, 1).Equals("1"))
                            {
                                areacode = number.Substring(1, 3);
                                prefix = number.Substring(4, 3);
                                suffix = number.Substring(7);
                                number = string.Concat("1 (", areacode, ")", prefix, "-", suffix);
                            }
                            break;
                        default:
                            break;
                    }

                    if (!string.IsNullOrWhiteSpace(_ext))
                    {
                        return string.Concat(number, " ext. ", _ext);
                    }
                    else
                    {
                        return number;
                    }
                }
            }
        }
        #endregion
    }
}